package com.wl.api.service.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.constant.ClientConstant;
import com.wl.api.dto.system.SecurityAreaLadderExtendDTO;
import com.wl.api.dto.system.SecurityChannelAreaLadderPriceDTO;
import com.wl.api.dto.system.SecurityCountryDTO;
import com.wl.api.mapper.system.SecurityChannelAreaLadderPriceMapper;
import com.wl.api.model.system.SecurityChannelAreaLadderPrice;
import com.wl.api.model.system.SecurityCountry;
import com.wl.api.service.system.ISecurityChannelAreaLadderPriceService;
import com.wl.api.service.system.ISecurityCountryService;

/**
 *
 * SecurityChannelAreaLadderPrice 表数据服务层接口实现类
 *
 */
@Service
public class SecurityChannelAreaLadderPriceServiceImpl extends ServiceImpl<SecurityChannelAreaLadderPriceMapper, SecurityChannelAreaLadderPrice> implements ISecurityChannelAreaLadderPriceService {
	
	@Autowired
	SecurityChannelAreaLadderPriceMapper areaLadderPriceMapper;
	@Autowired
	ISecurityCountryService countryService;
	
	@Transactional
	@Override
	public void insertLadderPriceBatch(List<SecurityChannelAreaLadderPrice> deliveryLadders, List<SecurityChannelAreaLadderPrice> pickUpLadders) {
		insertBatch(deliveryLadders);
		insertBatch(pickUpLadders);
		
	}
	
	@Transactional
	@Override
	public void updateLadderPriceBatch(List<SecurityChannelAreaLadderPrice> list, Integer countryId) {
		Assert.notEmpty(list);
		Assert.notNull(countryId);
		List<SecurityChannelAreaLadderPrice> source =  getAreaLadderPricesByParam(SecurityChannelAreaLadderPrice.builder().areaId(countryId).build());
		List<Long> delIds = new ArrayList<Long>();
		List<SecurityChannelAreaLadderPrice> updateList = list.parallelStream().filter(s -> !ObjectUtil.isEmpty(s.getId())).collect(Collectors.toList()); 
		List<SecurityChannelAreaLadderPrice> insertList = list.parallelStream().filter(s -> ObjectUtil.isEmpty(s.getId())).collect(Collectors.toList());
		//del 
		if (!CollectionUtil.isEmpty(source)) {
			for(SecurityChannelAreaLadderPrice sc: source) {
				if (CollectionUtil.isEmpty(updateList)) {
					delIds.add(sc.getId());
					continue;
				}
				if (updateList.parallelStream().noneMatch(s -> Objects.equals(s.getId(), sc.getId()))) {
					delIds.add(sc.getId());
					continue;
				}
			}
		}
		
		if(!CollectionUtil.isEmpty(delIds)) {
			deleteBatchIds(delIds);
		}
		if (!CollectionUtil.isEmpty(updateList)) {
			updateBatchById(updateList);
		}
		if (!CollectionUtil.isEmpty(insertList)) {
			insertBatch(insertList);
		}
		
	}

	public  List<SecurityChannelAreaLadderPrice> getAreaLadderPricesByParam(SecurityChannelAreaLadderPrice param) {
		return  selectList(new EntityWrapper<SecurityChannelAreaLadderPrice>(param));
	}

	@Override
	public List<SecurityAreaLadderExtendDTO> getItemList(SecurityAreaLadderExtendDTO param) {
		 
		return areaLadderPriceMapper.getAreaLadderList(param);
	}

	@Override
	public SecurityAreaLadderExtendDTO getAreaLadderDetail(SecurityAreaLadderExtendDTO param) {
		Assert.isTrue(param != null && param.getCountryId() != null);
		SecurityAreaLadderExtendDTO  data =  new SecurityAreaLadderExtendDTO(); 
		List<SecurityChannelAreaLadderPrice>  source =	getAreaLadderPricesByParam(SecurityChannelAreaLadderPrice.builder().areaId(param.getCountryId()).feeType(param.getFeeType()).build());
		 if(CollectionUtil.isEmpty(source)) {
			 return data;
		 }
		 SecurityCountry country = countryService.selectById(source.get(0).getAreaId());	
		 BeanUtils.copyProperties(country, data);
		 if (param.getFeeType() != null) {
			 if (Objects.equals(param.getFeeType(), ClientConstant.fee_type_delivery)) {
				 data.buildDeliveryLadder(source.parallelStream().filter(s -> Objects.equals(s.getFeeType(), ClientConstant.fee_type_delivery))
						 .map(s ->SecurityChannelAreaLadderPriceDTO.builder().id(s.getId()).areaId(s.getAreaId()).feeType(s.getFeeType()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).build())
						 .sorted((a, b) -> a.getWeightFrom().compareTo(b.getWeightFrom())).collect(Collectors.toList()));
			 }else if (Objects.equals(ClientConstant.FEE_TYPE_PICK_UP, param.getFeeType())) {
				 data.buildPickUpLadder( source.parallelStream().filter(s -> Objects.equals(s.getFeeType(), ClientConstant.FEE_TYPE_PICK_UP))
				 .map(s ->SecurityChannelAreaLadderPriceDTO.builder().id(s.getId()).areaId(s.getAreaId()).feeType(s.getFeeType()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).build())
				 .sorted((a, b) -> a.getWeightFrom().compareTo(b.getWeightFrom())).collect(Collectors.toList()));
			 }
			 
		 } else {
		 data.buildDetail(
				  source.parallelStream().filter(s -> Objects.equals(s.getFeeType(), 2))
				 .map(s ->SecurityChannelAreaLadderPriceDTO.builder().id(s.getId()).areaId(s.getAreaId()).feeType(s.getFeeType()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).build())
				 .sorted((a, b) -> a.getWeightFrom().compareTo(b.getWeightFrom())).collect(Collectors.toList()),
				 source.parallelStream().filter(s -> Objects.equals(s.getFeeType(), 1))
				 .map(s ->SecurityChannelAreaLadderPriceDTO.builder().id(s.getId()).areaId(s.getAreaId()).feeType(s.getFeeType()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).build())
				 .sorted((a, b) -> a.getWeightFrom().compareTo(b.getWeightFrom())).collect(Collectors.toList()) );
		 }
		 return data;
	}


}